package com.tqjc.execute.thread.aysn;

import com.tqjc.area.entity.SteelPlate;
import com.tqjc.area.service.SteelPlateService;
import com.tqjc.execute.crane.service.CraneInfoService;
import com.tqjc.execute.task.entity.Task;
import com.tqjc.execute.task.service.TaskService;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.system.core.util.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-06  16:16
 * @Description: 半自动任务数据保存线程
 * @Version: 1.0
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class AysnSemiAutoTaskSaveThread extends Thread{

    private SteelInfo steelInfo;

    private Date startDate;

    private String craneType;

    @Override
    public void run() {
        try {

            TaskService taskService = SpringUtils.getBean(TaskService.class);
            String type = "2#横移".equals(craneType) ? "online" : "offline";
            CraneInfoService craneInfoService = SpringUtils.getBean(CraneInfoService.class);
            Integer rollerId = craneInfoService.getRollerIdByCraneName(craneType);
            Task semiAutoTask = Task.builder().taskType("semiauto").craneType(craneType).beginTime(startDate).type(type)
                    .rollerId(rollerId).steelNo(steelInfo.getMatNo()).taskStatus(1).build();
            if(taskService.isExistSemiAutoTask(steelInfo.getMatNo(),craneType)){
                log.info("天车【{}】执行的任务数据已存在",craneType);
                return;

            }
            taskService.save(semiAutoTask);

            SteelPlate steelPlate = SteelPlate.builder().thickness(Math.round(steelInfo.getMatActThick())).steelId(steelInfo.getMatNo())
                    .length(Math.round(steelInfo.getMatActLen())).weight(Math.round(steelInfo.getMatActWt()))
                    .width(Math.round(steelInfo.getMatActWidth())).build();
            SteelPlateService steelPlateService = SpringUtils.getBean(SteelPlateService.class);
            steelPlateService.save(steelPlate);

        } catch (Exception e){
            log.error("天车【{}】半自动任务保存数据线程出现异常",craneType,e);

        }

    }
}
